package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.MyMoney;
import cn.wolfcode.trip.base.mapper.MyMoneyMapper;
import cn.wolfcode.trip.base.service.IMyMoenyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MyMoneyServiceImpl implements IMyMoenyService {
    @Autowired
    private MyMoneyMapper myMoneyMapper;

    @Override
    public MyMoney selectByUserId(Long userId) {
        return myMoneyMapper.selectByUserId(userId);
    }
    /**
     * 提现操作
     * @param userId
     * @param rmb
     */
    @Override
    public void updataByUserId(Long userId, Integer rmb) {
        MyMoney myMoney = myMoneyMapper.selectByUserId(userId);
       if(rmb > myMoney.getMoney()){
           throw new RuntimeException("提现余额大于存款,请充值");
       }
        myMoneyMapper.updataByUserId(userId, rmb);

    }

    /**
     * 充值操作
     * @param userId
     * @param rmb
     */
    @Override
    public void addmoneysUpdataByUserId(Long userId, Integer rmb) {
        MyMoney myMoney = myMoneyMapper.selectByUserId(userId);
        Integer money = myMoney.getMoney();
        if (rmb >= 0) {
            rmb = money + rmb;
            myMoneyMapper.addmoneysUpdataByUserId(userId, rmb);
        }
    }
}
